<?php 

class Event_ProductPreValidation extends Crud_Event_Handler 
{
	protected $_type = 'preValidation';
	
	/**
	 * Run handler with options array.
	 * 
	 * @param OrdersRow $row
	 * @return void
	 */
	public function run($row)
	{
/*		$weight = $this->_getParam('weight');
		if ($weight) {
			$value = $weight * 1000;
			$this->_setParam('weight', $value);
		}*/
		
		$values = $this->_getParam('product2product_category');
		$this->_setParam('product2product_category_final', $values);
		if (!$values) return;
		$rowset = ProductCategoryPeer::getInstance()->find($values);
		foreach ($rowset as $category) {
			$this->_selectParent($category, $values);
		}
		
		$values = array_unique($values);
		$this->_setParam('product2product_category', $values);
	}
	
	protected function _selectParent($row, &$values)
	{
		$parent = $row->getProductCategoryRowByParentId();
		if ($row->getParentId()) {
			$this->_selectParent($row->getProductCategoryRowByParentId(), $values);
		}
		
		$values[] = $row->getId();
	}
	
}
